package com.app.service.Impl;

import com.app.mapper.MerchantsMapper;
import com.app.model.MerchantsModel;
import com.app.service.MerchantsService;
import com.app.vo.MerchantsModelVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MerchantsServiceImpl implements MerchantsService {
    @Autowired
    MerchantsMapper merchantsMapper;

    //招商引资查询所有
    @Override
    public MerchantsModelVo getMerchantsList(Integer page, Integer limit) {
        Page<MerchantsModel> page1 = new Page<>(page, limit);
        Page<MerchantsModel> merchantsModelPage = merchantsMapper.selectPage(page1, null);
        List<MerchantsModel> merchantsList = merchantsModelPage.getRecords();
        int count = (int) merchantsModelPage.getTotal();
        MerchantsModelVo modelVo = new MerchantsModelVo();
        modelVo.setList(merchantsList);
        modelVo.setCount(count);
        return modelVo;
    }

    /**
     * 按页列出
     *
     * @param page         第页
     * @param limit        限制
     * @param queryWrapper 查询包装器
     * @return {@link Map}<{@link String}, {@link Object}>
     */
    @Override
    public Map<String, Object> listByPage(Integer page, Integer limit, QueryWrapper<MerchantsModel> queryWrapper) {

        Page<MerchantsModel> merchantsModelPage = new Page<>();
        merchantsModelPage.setCurrent(page);
        merchantsModelPage.setSize(limit);
        IPage<MerchantsModel> merchantsModelIPage = this.merchantsMapper.selectPage(merchantsModelPage, queryWrapper);
        Map<String, Object> response = new HashMap<>();
        response.put("list", merchantsModelIPage.getRecords());
        response.put("count", merchantsModelIPage.getTotal());
        return response;
    }

    @Override
    public void insert(MerchantsModel merchantsModel) {
        this.merchantsMapper.insert(merchantsModel);
    }

    @Override
    public void update(MerchantsModel merchantsModel) {
        MerchantsModel model = this.merchantsMapper.selectById(merchantsModel.getId());
        if (model != null) {
            this.merchantsMapper.updateById(merchantsModel);
        }
    }

    @Override
    public void del(Integer id) {
        QueryWrapper<MerchantsModel> merchantsModelQueryWrapper = new QueryWrapper<>();
        merchantsModelQueryWrapper.eq("id", id);
        this.merchantsMapper.delete(merchantsModelQueryWrapper);
    }

    @Override
    public MerchantsModel getMerchants(Integer id) {
        MerchantsModel merchantsModel = merchantsMapper.selectById(id);
        return merchantsModel;
    }
}
